'use strict';
const db = uniCloud.database();
const dbCmd = db.command ;
const $ = db.command.aggregate ;
const cate = db.collection("news-navbar");

module.exports = {
	
	save : async function(e){
		var data = this.getModel();		
		if (!data._id) {			
			await cate.add(data);
			return this.ok();
		}
		var d = this.findFirst(await cate.doc(data._id).get());
		if (null == d) {
			return this.fail("信息不存在");
		}		
		await this.updateById(cate , data);
		return this.ok();
	},
	info : async function(e){
		var id = this.params.id ;		
		if (!id) {
			return {};
		}
		var data = this.findFirst( await cate.doc(id).get() );
		return { data };
	},
	delete : async function(e){
		var id = this.params.id ;
		await cate.doc(id).remove();
		return this.ok();
	},
	list : async function(res){
		var dataInDB = await cate.field({
			"name" : 1 ,
			"icon":1,
			"url":1,
			"sort":1,
			"parent_id":1,
			"isshow":1
		}).orderBy("sort","asc").get() ;
		var list = this.find( dataInDB );
		
		return {list};
	},
}